using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using Styx;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Natfoth.Helpers;
using Styx.WoWInternals.World;
using TreeSharp;
using Action = TreeSharp.Action;

namespace Natfoth.Classes
{
    public class Druid
    {
        private static LocalPlayer Me { get { return ObjectManager.Me; } }
        private static WoWUnit Targ { get { return ObjectManager.Me.CurrentTarget; } }
        public static string Version = "0.0.1";
        public static string Name = "Akatosh Druid";

        public static Composite DruidRestBehavior()
        {
            return new PrioritySelector(

				Lib.Cast("Regrowth", akatosh => Me.HealthPercent <= 95 && !Me.IsInInstance && !Me.IsCasting && !Self.IsBuffOnMe("Regrowth")),
			   Lib.Cast("Nourish", akatosh => Me.HealthPercent <= DruidSettings.RestHealth && !Me.IsInInstance && !Me.IsCasting),
				
				Lib.RestBehaviour()

                    
            );
        }

		static bool lastWrath = false;

		private static string _oldDps = "Wrath";
		public static string GetBoomkinSpell()
		{
			if (Self.IsBuffOnMe("Eclipse (Solar)"))
			{
				_oldDps = "Wrath";


			}
			else if (Self.IsBuffOnMe("Eclipse (Lunar)"))
			{

				_oldDps = "Starfire";

			}
			
			return _oldDps;
		}

        public static Composite DruidMoonkinCombat()
        {
			lastWrath = !lastWrath;
			
			

            return new PrioritySelector(

                    new Decorator(ret => !Me.GotTarget && Me.GotAlivePet && Me.Pet.GotTarget, new Action(ret => Me.Pet.CurrentTarget.Target())),

                    new Lib.NeedToRetarget(new Lib.Retarget()),

                    new Lib.NeedToCheckCombatTimer(new Lib.CheckCombatTimer()),

                    new Lib.NeedToLOSCheck(new Lib.LOSCheck()),

                    new Lib.NeedToAutoAttack(new Lib.AutoAttack()),

                    new Decorator(akatosh => Targ == null,
                        new Action(akatosh => RunStatus.Success)),

                    new Decorator(akatosh => Targ != null && !Me.IsFacing(Targ),
                        new Action(akatosh => Lib.FaceTarget())),

					new Decorator(akatosh => Targ != null && Targ.Distance < 35 && Me.IsMoving,
						new Action(akatosh => Navigator.PlayerMover.MoveStop())),

                        //Spells Here



						Lib.Cast("Regrowth", akatosh => Me.HealthPercent <= DruidSettings.RestHealth && !Me.IsInInstance && !Me.IsCasting && !Self.IsBuffOnMe("Regrowth")),


						Lib.Cast("Moonkin Form", akatosh => !Self.IsBuffOnMe("Moonkin Aura")),
						
						Lib.Cast("Moonfire", akatosh => (!Targ.HasAura("Moonfire") && !Targ.HasAura("Sunfire"))),

						Lib.Cast("Insect Swarm", akatosh => Targ.GetAuraTimeLeft("Insect Swarm", true).Seconds < 2 || (!Targ.HasAura("Insect Swarm"))),

						Lib.Cast("Starsurge", akatosh => !Spell.IsOnCooldown("Starsurge") && Self.IsBuffOnMe("Shooting Stars")),
						
						Lib.Cast("Wrath", akatosh => GetBoomkinSpell() == "Wrath"),
						Lib.Cast("Starfire", akatosh => GetBoomkinSpell() == "Starfire"),
 
                        //End Spells





					Lib.CreateMoveToTargetBehavior(true, 35),

                    new Action(ret => ObjectManager.Update())
             );

			
        }

       

        public static Composite DruidCombatBuffs()
        {
            return new PrioritySelector(
				//Lib.Cast("Mark of the Wild", ret => !Self.IsBuffOnMe("Mark of the Wild"))
            );
        }

        public static bool DruidNeedBuffs()
        {
            /*if (!Lib.HasBuff("Inner Fire"))
                return true;

            if (!Lib.HasBuff("Power Word: Fortitude"))
                return true;*/

            return false;
        }

        public static Composite DruidBuffs()
        {
             return new PrioritySelector(

				 Lib.Cast("Mark of the Wild", ret => !Self.IsBuffOnMe("Mark of the Wild")),
				 Lib.Cast("Moonkin Form", akatosh => !Self.IsBuffOnMe("Moonkin Aura"))
            );
        }


        

        public static Composite DruidPullBehavior()
        {
            return new PrioritySelector(

                new Lib.NeedToNavigatePath(new Lib.NavigatePath()),

                //new Lib.NeedToBlacklistPullTarget(new Lib.BlacklistPullTarget()),

                //new Lib.NeedToCheckPullTimer(new Lib.BlacklistPullTarget()),
				new Decorator(akatosh => Targ == null,
						new Action(akatosh => RunStatus.Success)),

                new Lib.NeedToLOSCheck(new Lib.LOSCheck()),

                new Decorator(akatosh => Targ != null && !Me.IsFacing(Targ),
                    new Action(akatosh => Lib.FaceTarget())),

					new Decorator(akatosh => Targ != null && Targ.Distance < 35 && Me.IsMoving,
						new Action(akatosh => Navigator.PlayerMover.MoveStop())),

                new Lib.NeedToAutoAttack(new Lib.AutoAttack()),


               //Pull Spells


			   Lib.Cast("Wrath"),


               //End Pull Spells

				Lib.CreateMoveToTargetBehavior(true, 35),

				

                new Action(ret => ObjectManager.Update())
            );
        }

       

        public static void DruidClassPulse()
        {
            //int lootableMobs = LootTargeting.Instance.LootingList.Count;

            // If ShamanSettings.DirtyData = true it will reload the settings from the XML file
            // This reads the XML file and re-populates the ShamanSettings class with any changed values
			if (!AkatoshCore.Instance._isCCLoaded) { AkatoshCore.Instance._isCCLoaded = true; DruidSettings.DirtyData = true; }
            if (DruidSettings.DirtyData) AkatoshCore.Instance.LoadSettings(true);

            // So we don't overload HB the below code is only run once per second
            if (!Timers.Expired("Pulse", 1000)) return;
            Timers.Recycle("Pulse", 1000);

            // Environmental ShamanSettings
            if (Timers.Expired("EnvironmentSettings", 5000))
            {
				if (DruidSettings.MultipleEnvironment.Contains("never"))
                {
                    ConfigSettings.CurrentEnvironment = "PVE";
                }
                else
                {
                    Timers.Reset("EnvironmentSettings");
                    string environment = Utils.IsBattleground ? "PVP" : "PVE";
                    environment = ObjectManager.Me.IsInInstance ? "Instance" : environment;
                    if (!ConfigSettings.UIActive && environment != ConfigSettings.CurrentEnvironment)
                    {
                        ConfigSettings.CurrentEnvironment = environment;
                        Utils.Log(string.Format("*** Environment has changed. Loading {0} settings.", environment), Utils.Colour("Red"));
                        AkatoshCore.Instance.LoadSettings(false);
                    }
                }
            }

           
        }
    }

	
}